<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
require_once ('./application/controllers/authenticate.php');
class Zone extends Authenticate {
	
	const RECORD_PAGE_RANGE = 5;
	const RECORD_PER_PAGE = 20;
	
	function __construct()
	{
		parent::__construct();
		$this->load->model('adv_zone_model');
		$this->load->library('html/control/fx_control');
		$this->load->library('html/control/fx_filter');
		$this->load->library('html/control/fx_sort');
		$this->adv_zone_model->addAttributeOrder('id', 'desc');
		$this->load->library('upload');
		// Load Zone Model
		$this->loadCurrentZone();
	}

	public function loadCurrentZone() 
	{
		try {
			// Load Zone Model
			$id = $this->input->getParam('id');
			if ($id) {
				$this->adv_zone_model->loadByPK($id);
			}
			return $this->adv_zone_model;
		} catch (Exception $ex) {
			return null;
		}
	}
	public function index() 
	{	
		try {
			//Unload Current Zone
			$this->st_registry->clearRegistry('admin/adv/zone/id');
			//Get Data
			$data = $this->_initGridView();
			//Transfer To View
			$this->load->view('adv/zone', $data);
		} catch (Exception $ex) {
			echo $ex->getMessage();
		}
	}
	
	public function edit() 
	{
		try {
			$data['model'] = $this->adv_zone_model;
			$this->load->view('adv/zone/edit', $data);	
		} catch (Exception $ex) {
			echo $ex->getMessage();
		}
	}
	
	protected function _initGridView()
	{
		$this->adv_zone_model->resetFilterList();
		// Get Paginator Params
		$page = $this->input->getParam('page');
		$recordPerPage = $this->input->getParam('recordPerPage');
		if (!$recordPerPage) {
			$recordPerPage = self::RECORD_PER_PAGE;
		}
		// Get Filter Params	
		$filters = $this->input->post('zoneFilter');
		// Filter
		$this->fx_filter->filter($filters, $this->adv_zone_model);
		// Get Order Params
		$order = $this->input->post('zoneOrder');
		// Sort
		$this->fx_sort->sort($order, $this->adv_zone_model);
		// Get total found records
		$found = $this->adv_zone_model->countTotal();
		// Paginate
		$this->load->library("tree/paginator/engine/st_paginator_collection");
		$this->load->library("tree/paginator/st_paginator");
		$this->st_paginator_collection->setModel($this->adv_zone_model);
		$this->st_paginator->setConfig(array(
			'engine' => $this->st_paginator_collection,
			'pageRange' => self::RECORD_PAGE_RANGE,
			'itemCountPerPage' => $recordPerPage,
			'currentPageNumber' => $page  
		));		
		// Paginate Collection
		$collection = $this->st_paginator->getItemsByPage($page);
		if (empty($collection)) {
			$collection = array();
		}
		$data = array();
		$data['zone_selected'] = $this->input->post('zone_selected');
		$data['found'] = $found;
		$data['zone_ids'] = $this->input->post('zone_ids');
		$data['category_id'] = $this->input->post('category_id');
		$data['zone_ids_array'] = explode(',', $this->input->post('zone_ids'));
		
		/*END RADIO SELECTED*/
		$data['model'] = $this->adv_zone_model;
		$data['sorter'] = $this->fx_sort;
		$data['filters'] = $this->fx_filter;
		$data['paginator'] = $this->st_paginator;
		$data['collection'] = $collection;
		return $data;
	}
	 
	public function save() 
	{
		try {
			// Load Zone Model
			$id = $this->input->post('id');
			if ($id) {
				$this->adv_zone_model->loadByPK($id);
			}
			$data = $this->input->post('zone');
			if (is_array($data) && count($data) > 0) {
				if (strlen($data['code']) == 0) {
					$data['code'] = str_replace(' ', '-', $data['name']);
				}
				$this->adv_zone_model->setEntireData($data);
				$this->adv_zone_model->save();
				$id = $this->adv_zone_model->getId();
				$this->st_registry->register('admin/adv/zone/message', "Success: Save Successfully");
			}
			if ($id) {
				redirect(site_url("admin/adv/zone/edit/id/".$this->adv_zone_model->getId(), true));
			} else {
				redirect(site_url("admin/adv/zone/index", true));
			}
		} catch (Exception $ex) {
			$this->st_registry->register('admin/adv/zone/message', "Error: when deleting zone");
			redirect(site_url("admin/adv/zone/index", true));
		}
	}
	
	public function delete()
	{
		try {
			if ($this->adv_zone_model->isLoaded()) {
				$name = $this->adv_zone_model->getTitle();
				$this->adv_zone_model->delete();
				$this->st_registry->register('admin/adv/zone/message', "Delete zone $name successfully");
				redirect(site_url("admin/adv/zone/index", true));
			}
			else {
				$this->st_registry->register('admin/adv/zone/message', "Cannot delete zone");
				redirect(site_url("admin/adv/zone/index", true));
			}
		} catch (Exception $ex) {
			$this->st_registry->register('admin/adv/zone/message', "Error when deleting zone");
			redirect(site_url("admin/adv/zone/index", true));
		}
	}
	
	public function massVisible()
	{
		try {
			$ids = $this->input->getMassValues('zone_ids');
			if ($ids) {
				$this->adv_zone_model->query("update adv_zone set visible = 1 where id in ($ids)");
			}
			$countItem = count(explode(',', $ids));
			$this->st_registry->register('admin/adv/zone/message', "Set [$countItem] zones visible successfully");
			redirect(site_url("admin/adv/zone/index", true));
		} catch (Exception $ex) {
			$this->st_registry->register('admin/adv/zone/message', $e->getMessage());
			redirect(site_url("admin/adv/zone/index", true));
		}
	}
	
	public function massHiding()
	{
		try {
			$ids = $this->input->getMassValues('zone_ids');
			if ($ids) {
				$this->adv_zone_model->query("update adv_zone set visible = 0 where id in ($ids)");
			}
			$countItem = count(explode(',', $ids));
			$this->st_registry->register('admin/adv/zone/message', "Set [$countItem] zones hided successfully");
			redirect(site_url("admin/adv/zone/index", true));
		} catch (Exception $ex) {
			$this->st_registry->register('admin/adv/zone/message', $e->getMessage());
			redirect(site_url("admin/adv/zone/index", true));
		}
	}
	
	public function massDelete()
	{
		try {
			$ids = $this->input->getMassValues('zone_ids');
			if ($ids) {
				$this->adv_zone_model->query("delete from adv_zone where id in ($ids)");
			}
			$countItem = count(explode(',', $ids));
			$this->st_registry->register('admin/adv/zone/message', "Delete [$countItem] zones successfully");
			redirect(site_url("admin/adv/zone/index", true));
		} catch (Exception $ex) {
			$this->st_registry->register('admin/adv/zone/message', $e->getMessage());
			redirect(site_url("admin/adv/zone/index", true));
		}
	}
}